package com.xzl.xep.dao.hibernate;

import org.springframework.stereotype.Repository;

import com.xzl.xep.common.utils.DateUtil;
import com.xzl.xep.common.utils.StringUtil;
import com.xzl.xep.dao.BusinessDao;
import com.xzl.xep.pojo.Customer;
import com.xzl.xep.pojo.Operator;
import com.xzl.xep.pojo.Payment;
import com.xzl.xep.pojo.Company;

@Repository("business")
public class HibernateBusinessDao extends HibernateDao implements BusinessDao{

	@Override
	public Payment plusCash(Operator operator, Double money,String remark) {
		return cashFlow(operator, money, 1,remark);
	}

	@Override
	public Payment minusCash(Operator operator, Double money,String remark) {
		return cashFlow(operator, money, 0,remark);
	}
	
	
	private Payment cashFlow(Operator operator, Double money,Integer type,String remark){
		Payment cash = new Payment();
		cash.setCompanyId(operator.getCompanyId());
		cash.setBusiness(Payment.OTHER_BUSINESS);
		cash.setOperatorId(operator.getId());
		cash.setOperatorName(operator.getRealName());
		cash.setMoney(money);
		cash.setType(type);
		cash.setWay(Payment.CASH_WAY);
		cash.setCreateTime(DateUtil.getCurrentDate());
		cash.setModifyTime(DateUtil.getCurrentDate());
		
		
		if(StringUtil.isEmpty(remark)){
			if(type == 1)
				remark = "存放";
			else
				remark = "支取";
		}
		cash.setRemark(remark);	
		
		this.saveObject(cash);
		
		Company company = this.loadObject(Company.class, operator.getCompanyId());
		if(type == 1)
			company.plusCash(money);
		else
			company.minusCash(money);
		
		this.updateObject(company);
		
		return cash;
		
	}

	@Override
	public Customer credit(Customer customer, Double money, Integer way,
			Operator operator) {
		customer.setBalance(customer.getBalance()+money);
		customer.setTotalCredit(customer.getTotalCredit()+money);
		customer.setLastCreditTime(DateUtil.getCurrentDate());
		
		this.updateObject(customer);
		
		Payment pay = new Payment();
		pay.setCompanyId(operator.getCompanyId());
		pay.setBusiness(Payment.CREDIT_BUSINESS);
		pay.setOperatorId(operator.getId());
		pay.setOperatorName(operator.getRealName());
		pay.setMoney(money);
		pay.setType(1);
		switch(way){
			case 0 : pay.setWay(Payment.CASH_WAY); pay.setRemark("会员-现金充值"); break;
			case 1 : pay.setWay(Payment.ALIPAY_WAY);  pay.setRemark("会员-支付宝充值");break;
			case 2 : pay.setWay(Payment.BANK_WAY);  pay.setRemark("会员-银联充值");break;
		}
		pay.setCreateTime(DateUtil.getCurrentDate());
		pay.setModifyTime(DateUtil.getCurrentDate());
		pay.setCustomerId(customer.getCompanyId());
		pay.setCreditMoney(money);
		this.saveObject(pay);
		
		return customer;
	}

	
}
